package com.jfl.web.controller.system;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jfl.annotation.Log;
import com.jfl.base.AbstractController;
import com.jfl.common.Constants;
import com.jfl.core.utils.ShiroUtils;
import com.jfl.enums.LogType;
import com.jfl.enums.Module;
import com.jfl.exception.BusinessException;
import com.jfl.model.SysMenu;
import com.jfl.model.SysRole;
import com.jfl.system.SysMenuService;

/**
 * 菜单管理
 * 
 * @author 赵炎
 * @version [V1.00, 2018年12月11日]
 * @since V1.00
 */
@Controller
@RequestMapping("${jfl.module.system}/menu")
public class SysMenuController extends AbstractController
{
    @Reference(version = "${jfl.version}")
    private SysMenuService sysMenuService;
    
    @Value("${jfl.demoMode}")
    private boolean mode;
    /**
     * 菜单跳转页面
     * 
     * @param modelMap
     * @return
     */
    @RequiresPermissions("system:menu:view")
    @GetMapping
    public String dept(ModelMap modelMap)
    {
        return Constants.MODULE_SYS_MENU_PREFIX + "menu";
    }
    
    /**
     * 菜单列表
     * 
     * @param menu
     * @return
     */
    @RequiresPermissions("system:menu:list")
    @GetMapping("/list")
    @ResponseBody
    public List<SysMenu> list(SysMenu menu)
    {
        List<SysMenu> menuList = this.sysMenuService.queryList(menu);
        return menuList;
    }
    
    /**
     * 修改
     * 
     * @param sysDept
     *            部门对象
     * @return
     */
    @GetMapping("/edit/{menuId}")
    public String edit(@PathVariable("menuId") Long menuId, ModelMap modelMap)
    {
        modelMap.put("menu", this.sysMenuService.queryById(menuId));
        return Constants.MODULE_SYS_MENU_PREFIX + "edit";
    }
    
    /**
     * 修改保存菜单
     */
    @Log(module = Module.MENU, value = "修改菜单", type = LogType.UPDATE)
    @RequiresPermissions("system:menu:edit")
    @PostMapping("/edit")
    @ResponseBody
    public ResponseEntity<ModelMap> editSave(SysMenu menu)
    {
        if(mode) 
        {
            throw new BusinessException("演示模式，不允许操作！");
        }
        menu.setUpdateBy(ShiroUtils.getCurrentUserId());
        this.sysMenuService.update(menu);
        ShiroUtils.clearCachedAuthorizationInfo();
        return setSuccessModelMap();
    }
    
    /**
     * 新增 跳转新增页面并加载上级菜单
     * 
     * @param parentId
     * @param mmap
     * @return
     */
    @GetMapping("/add/{parentId}")
    public String add(@PathVariable("parentId") Long parentId, ModelMap mmap)
    {
        SysMenu menu = null;
        // 根目录
        if (0L != parentId)
        {
            menu = this.sysMenuService.queryById(parentId);
        }
        else
        {
            menu = new SysMenu();
            menu.setId(0L);
            menu.setMenuName("主目录");
        }
        mmap.put("menu", menu);
        return Constants.MODULE_SYS_MENU_PREFIX + "add";
    }
    
    /**
     * 新增保存菜单
     * 
     * @param menu
     * @return
     */
    @Log(module = Module.MENU, value = "添加菜单", type = LogType.INSERT)
    @RequiresPermissions("system:menu:add")
    @PostMapping("/add")
    @ResponseBody
    public ResponseEntity<ModelMap> addSave(SysMenu menu)
    {
        if(mode) 
        {
            throw new BusinessException("演示模式，不允许操作！");
        }
        menu.setCreateBy(ShiroUtils.getCurrentUserId());
        this.sysMenuService.update(menu);
        ShiroUtils.clearCachedAuthorizationInfo();
        return setSuccessModelMap();
    }
    
    /**
     * 删除
     * 
     * @param menuId
     *            菜单Id
     * @return
     */
    @Log(module = Module.MENU, value = "删除菜单", type = LogType.DELETE)
    @RequiresPermissions("system:menu:remove")
    @PostMapping("/remove/{menuId}")
    @ResponseBody
    public ResponseEntity<ModelMap> delete(@PathVariable("menuId") Long menuId)
    {
        if(mode) 
        {
            throw new BusinessException("演示模式，不允许操作！");
        }
        this.sysMenuService.deleteById(menuId);
        ShiroUtils.clearCachedAuthorizationInfo();
        return setSuccessModelMap();
    }
    
    /**
     * 加载角色菜单列表树
     * 
     * @param role
     * @return
     */
    @GetMapping("/roleMenuTreeData")
    @ResponseBody
    public List<Map<String, Object>> roleMenuTreeData(SysRole role)
    {
        List<Map<String, Object>> tree = this.sysMenuService.roleMenuTreeData(role);
        return tree;
    }
    
    /**
     * 选择菜单图标
     * 
     * @return
     */
    @GetMapping("/icon")
    public String icon()
    {
        return Constants.MODULE_SYS_MENU_PREFIX + "icon";
    }
    
    /**
     * 选择菜单树
     * 
     * @param menuId
     * @param mmap
     * @return
     */
    @GetMapping("/selectMenuTree/{menuId}")
    public String selectMenuTree(@PathVariable("menuId") Long menuId, ModelMap mmap)
    {
        mmap.put("menu", this.sysMenuService.queryById(menuId));
        return Constants.MODULE_SYS_MENU_PREFIX + "tree";
    }
    
    /**
     * 加载所有菜单列表树
     * 
     * @param role
     * @return
     */
    @GetMapping(value = "/menuTreeData", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public List<Map<String, Object>> menuTreeData(SysRole role)
    {
        List<Map<String, Object>> tree = this.sysMenuService.menuTreeData();
        return tree;
    }
    
}
